package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class JoinServlet
 */
public class JoinServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JoinServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf8");
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		
		String nextPage=null;
		ResultSet rs = null;

		System.out.println("join come");

		Connection conn;

		String userId = request.getParameter("userid");
		String userPw = request.getParameter("userpw");
		String userName = request.getParameter("username");
		
		System.out.println("name 한글? "+ userName);
		
		try {
			conn = process.DBConnectionPool.getInstance().getConnetion();
		
			String sql = "select * from user where id = ?;";
			PreparedStatement st;
	
			st = conn.prepareStatement(sql);
		
		    st.setString(1, userId);
		    rs = st.executeQuery();

		
		
		//아이디 존재 x -> 회원가입 가능
		if(!rs.next()){
			
			nextPage="./index.jsp";
			
			conn = process.DBConnectionPool.getInstance().getConnetion();
			
			sql = "insert into user (id,pw,name) values (?,?,?);";
	
			st = conn.prepareStatement(sql);
		
		    st.setString(1, userId);
		    st.setString(2, userPw);
		    st.setString(3, userName);
		    st.executeUpdate();
		    
			session.setAttribute("id",userId);
			session.setAttribute("pw",userPw);
			session.setAttribute("name",userName);
			
			response.sendRedirect(nextPage);
		}
		//아이디가 이미 존재함
		else{
			out.println("id already exists");
			nextPage="./index.jsp";
			response.sendRedirect(nextPage);
			
			
			String cor_pw = rs.getString("pw");
			
			//로그인 성공
			if(cor_pw.equals(userPw)){
				session.setAttribute("id",userId);
				session.setAttribute("pw",userPw);
				session.setAttribute("score", rs.getString("score"));
				//map 세션에 넣을 때 배열로? 우선은 스트링으로
				session.setAttribute("map", rs.getString("map"));
				
				nextPage="./jsp/stage.html";
				response.sendRedirect(nextPage);
				//request.getRequestDispatcher(nextPage).forward(request, response);
				
			}
			//패스워드 불일치
			else{
				nextPage="./index.html";
				response.sendRedirect(nextPage);
			}
			
		}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}


}
